package com.chii.colorization.ui.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.os.Environment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;

import com.bumptech.glide.Glide;
import com.github.chrisbanes.photoview.PhotoView;
import com.google.android.material.snackbar.Snackbar;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;

public class PhotoAdapter extends PagerAdapter {
    private AppCompatActivity context;
    private List<String> imageList;

    public PhotoAdapter(AppCompatActivity context, List<String> imageList) {
        this.context = context;
        this.imageList = imageList;
    }

    @Override
    public int getCount() {
        return imageList.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        String url = imageList.get(position);
//        ImageView imageView = new ImageView(context);
//        Glide.with(context).load(url).dontAnimate().into(imageView);
//        container.addView(imageView);
//        imageView.setOnClickListener(v -> context.finish());
//        return imageView;

        PhotoView photoView = new PhotoView(context);
        Glide.with(context).load(url).dontAnimate().into(photoView);
        photoView.setOnClickListener(v -> context.finish());
        photoView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {

                photoView.setDrawingCacheEnabled(true);
                Bitmap bitmap = Bitmap.createBitmap(photoView.getDrawingCache());
                photoView.setDrawingCacheEnabled(false);
//                saveMyBitmap(context, bitmap);

                //弹出的“保存图片”的Dialog
                //长按后显示的 Item
                final String[] items = new String[]{"保存图片"};
                AlertDialog dialog = new AlertDialog.Builder(context)
                        .setItems(items,  new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                if (i == 0) {
                                    if (bitmap != null) {
                                        saveMyBitmap(context, bitmap);
                                    } else {
                                        Toast.makeText(context, "没有照片", Toast.LENGTH_SHORT)
                                        .show();
                                    }
                                }
                            }
                        }).create();

                dialog.show();

                return true;
            }
        });
        container.addView(photoView);

        return photoView;

    }

    private void saveMyBitmap(AppCompatActivity context, Bitmap bitmap) {
        // 首先保存图片 创建文件夹
        String sdCardDir = Environment.getExternalStorageDirectory() + "/DCIM/";
        File appDir = new File(sdCardDir, "color");
        if (!appDir.exists()) {//不存在
            appDir.mkdir();
        }
        //图片文件名称
        String fileName = "color_" + System.currentTimeMillis() + ".jpg";
        File file = new File(appDir, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 最后通知图库更新
        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        Uri uri = Uri.fromFile(file);
        intent.setData(uri);
        context.sendBroadcast(intent);
        Toast.makeText(context, "图片保存成功", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
}
